package prime.test.ajax;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;

@ManagedBean
@ViewScoped
public class AjaxBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8641424633240200486L;
	private String engineName;
	private Integer engineCount;
	private List<BookTest> books;
	private BookTest book;
	private BookTest bookSelected;

	public AjaxBean() {
		engineCount = 0;
		book = new BookTest();

		for (int i = 0; i < 30; i++) {
			getBooks().add(new BookTest("Autor " + i, "Titulo " + i));
		}
	}

	public String getEngineName() {
		return engineName;
	}

	public void setEngineName(String engineName) {
		this.engineName = engineName;
	}

	public Integer getEngineCount() {
		return engineCount;
	}

	public void setEngineCount(Integer engineCount) {
		this.engineCount = engineCount;
	}

	public void countListener(ActionEvent event) {
		engineCount++;
	}

	public List<BookTest> getBooks() {
		if (books == null)
			books = new LinkedList<BookTest>();

		return books;
	}

	public void setBooks(List<BookTest> books) {
		this.books = books;
	}

	public BookTest getBook() {
		return book;
	}

	public void setBook(BookTest book) {
		this.book = book;
	}

	public BookTest getBookSelected() {
		return bookSelected;
	}

	public void setBookSelected(BookTest bookSelected) {
		this.bookSelected = bookSelected;
	}

	public void addLibroListener() {
		getBooks().add(book);
		System.out.println(book.getTitulo());
		book = new BookTest();// reset
	}
	
	public void removeBookListener() {
		books.remove(bookSelected);
		System.out.println("removiendo endo...");
		getBook();
	}
}
